<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="二级模块" prop="secondLevel">
        <el-input
          v-model="queryParams.secondLevel"
          placeholder="请输入二级模块"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="三级模块名" prop="thirdLevel">
        <el-input
          v-model="queryParams.thirdLevel"
          placeholder="请输入三级模块名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="模块版本" prop="version">
        <el-input
          v-model="queryParams.version"
          placeholder="请输入模块版本"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['ai:moduleRegistry:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ai:moduleRegistry:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ai:moduleRegistry:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['ai:moduleRegistry:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="moduleRegistryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="模块ID" align="center" prop="moduleId" />
      <el-table-column label="二级模块" align="center" prop="secondLevel" />
      <el-table-column label="三级模块名" align="center" prop="thirdLevel" />
      <el-table-column label="模块版本" align="center" prop="version" />
      <el-table-column label="入口类型" align="center" prop="endpointType" />
      <el-table-column label="入口地址" align="center" prop="endpointUrl" />
      <el-table-column label="HTTP 方法" align="center" prop="method" />
      <el-table-column label="输入结构" align="center" prop="inputSchema" />
      <el-table-column label="输出结构" align="center" prop="outputSchema" />
      <el-table-column label="默认参数/阈值" align="center" prop="defaultParams" width="120px"/>
      <el-table-column label="责任组/负责人" align="center" prop="owner" width="120px"/>
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="备注" align="center" prop="notes" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['ai:moduleRegistry:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['ai:moduleRegistry:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改三级模块目录对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="moduleRegistryRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="模块ID" prop="moduleId">
          <el-input v-model="form.moduleId" placeholder="请输入模块ID" />
        </el-form-item>
        <el-form-item label="二级模块" prop="secondLevel">
          <el-input v-model="form.secondLevel" placeholder="请输入二级模块" />
        </el-form-item>
        <el-form-item label="三级模块名" prop="thirdLevel">
          <el-input v-model="form.thirdLevel" placeholder="请输入三级模块名" />
        </el-form-item>
        <el-form-item label="模块版本" prop="version">
          <el-input v-model="form.version" placeholder="请输入模块版本" />
        </el-form-item>
        <el-form-item label="入口类型" prop="endpointType">
          <el-input v-model="form.endpointType" placeholder="请输入入口类型" />
        </el-form-item>
        <el-form-item label="入口地址" prop="endpointUrl">
          <el-input v-model="form.endpointUrl" placeholder="请输入入口地址" />
        </el-form-item>
        <el-form-item label="HTTP 方法" prop="method">
          <el-input v-model="form.method" placeholder="请输入HTTP 方法" />
        </el-form-item>
        <el-form-item label="输入结构" prop="inputSchema">
          <el-input v-model="form.inputSchema" placeholder="请输入输入结构" />
        </el-form-item>
        <el-form-item label="输出结构" prop="outputSchema">
          <el-input v-model="form.outputSchema" placeholder="请输入输出结构" />
        </el-form-item>
        <el-form-item label="默认参数/阈值" prop="defaultParams">
          <el-input v-model="form.defaultParams" placeholder="请输入默认参数/阈值" />
        </el-form-item>
        <el-form-item label="责任组/负责人" prop="owner">
          <el-input v-model="form.owner" placeholder="请输入责任组/负责人" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-input v-model="form.status" placeholder="请输入状态" />
        </el-form-item>
        <el-form-item label="备注" prop="notes">
          <el-input v-model="form.notes" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="ModuleRegistry">
import { listModuleRegistry, getModuleRegistry, delModuleRegistry, addModuleRegistry, updateModuleRegistry } from "@/api/slope/ai/moduleRegistry";

const { proxy } = getCurrentInstance();

const moduleRegistryList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    secondLevel: null,
    thirdLevel: null,
    version: null,
  },
  rules: {
    moduleId: [
      { required: true, message: "模块ID不能为空", trigger: "blur" }
    ],
    secondLevel: [
      { required: true, message: "二级模块不能为空", trigger: "blur" }
    ],
    thirdLevel: [
      { required: true, message: "三级模块名不能为空", trigger: "blur" }
    ],
    version: [
      { required: true, message: "模块版本不能为空", trigger: "blur" }
    ],
    endpointType: [
      { required: true, message: "入口类型：sync/async/batch不能为空", trigger: "blur" }
    ],
    endpointUrl: [
      { required: true, message: "入口地址不能为空", trigger: "blur" }
    ],
    method: [
      { required: true, message: "HTTP 方法不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询三级模块目录列表 */
function getList() {
  loading.value = true;
  listModuleRegistry(queryParams.value).then(response => {
    moduleRegistryList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    moduleId: null,
    secondLevel: null,
    thirdLevel: null,
    version: null,
    endpointType: null,
    endpointUrl: null,
    method: null,
    inputSchema: null,
    outputSchema: null,
    defaultParams: null,
    owner: null,
    status: null,
    notes: null
  };
  proxy.resetForm("moduleRegistryRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.moduleId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加三级模块目录";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _moduleId = row.moduleId || ids.value
  getModuleRegistry(_moduleId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改三级模块目录";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["moduleRegistryRef"].validate(valid => {
    if (valid) {
      if (form.value.moduleId != null) {
        updateModuleRegistry(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addModuleRegistry(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _moduleIds = row.moduleId || ids.value;
  proxy.$modal.confirm('是否确认删除三级模块目录编号为"' + _moduleIds + '"的数据项？').then(function() {
    return delModuleRegistry(_moduleIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('ai/moduleRegistry/export', {
    ...queryParams.value
  }, `moduleRegistry_${new Date().getTime()}.xlsx`)
}

getList();
</script>
